

#include "stdafx.h"
#include "draft.h"

//! AVdataBuffer, used to store a sample from 
/*!
*/
AVdataBuffer::AVdataBuffer(AV_TYPE avtype)
:m_avType(avtype),m_Video(0),m_Audio(0),m_pdata(0)
{	
}


AVdataBuffer::~AVdataBuffer()
{
	if(m_pdata)av_free(m_pdata);
	if(m_Video)av_free(m_Video);
	delete m_Audio;
}

bool AVdataBuffer::AllocPicture(PixelFormat fmt, int w, int h)
{
	if(m_avType != video_type) return false;

	m_Video = (AVPicture*)avcodec_alloc_frame();
	if (!m_Video) return false;

	int size = avpicture_get_size(fmt, w, h);
	m_pdata =(uint8_t*)av_malloc(size);
	if (!m_pdata) {
		av_free(m_Video);
		m_Video = 0;
		return false;
	}

	avpicture_fill((AVPicture *)m_Video, m_pdata,fmt, w, h);

	return true;
}

bool AVdataBuffer::AllocAudio(int len)
{
	if(m_avType != audio_type) return false;

	m_pdata = (uint8_t*)av_malloc(len);
	if(!m_pdata) return false;

	m_Audio = new AVAudioFrame;
	if(!m_Audio){
		av_free(m_pdata);
		m_pdata = NULL;
		return false;
	}
	m_Audio->data = m_pdata;
	m_Audio->length = len;
	return true;
}

AVPicture* AVdataBuffer::GetPicture()
{
	return m_Video;
}

AVAudioFrame* AVdataBuffer::GetAudio()
{
	return m_Audio;
}




